import * as TYPES from '../action-types'

const initialState = {
	taskList: null,
}

const taskReducer = (state = initialState, action) => {
	state = JSON.parse(JSON.stringify(state))
	switch (action.type) {
		case TYPES.TASK_LIST:
			state.taskList = action.list
			break
		case TYPES.TASK_ADD:
			state.taskList.push(action.task)
			break
		case TYPES.TASK_UPDATE:
			let { taskList } = state
			if (Array.isArray(taskList)) {
				state.taskList = taskList.map((item) => {
					if (item.id === action.task.id) {
						item.state = 2
						item.complete = new Date().toLocaleString('zh-CN', { hour12: false })
					}
					return item
				})
			}
			break
		case TYPES.TASK_REMOVE:
			state.taskList = state.taskList.filter((item) => item.id !== action.id)
			break
	}

	return state
}

export default taskReducer